
import 'package:easy_loading_button/easy_loading_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/indent/iview/indent_list_iview.dart';
import 'package:flutter_deer/indent/presenter/indent_list_presenter.dart';
import 'package:flutter_deer/indent/provider/indent_list_page_provider.dart';
import 'package:flutter_deer/index/index_router.dart';
import 'package:flutter_deer/index/models/user_betting_entity.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/widgets/fadein_text.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/util/theme_utils.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:intl/intl.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
import 'package:text_scroll/text_scroll.dart';

import '../indent_router.dart';

class IndentListPage extends StatefulWidget {

  const IndentListPage({
    super.key,
    required this.index
  });

  final int index;

  @override
  _IndentListPageState createState() => _IndentListPageState();
}

class _IndentListPageState extends State<IndentListPage> with AutomaticKeepAliveClientMixin<IndentListPage>,BasePageMixin<IndentListPage, PowerPresenter>, SingleTickerProviderStateMixin implements IndentListIMvpView {

  late IndentListPagePresenter indentListPagePresenter;
  IndentListPageProvider provider = IndentListPageProvider();
  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    indentListPagePresenter = new IndentListPagePresenter();
    powerPresenter.requestPresenter([indentListPagePresenter]);
    return powerPresenter;
  }

  @override
  void setUserBettingPage(UserBettingPageEntity? pageEntity){
    provider.setBettingPage(pageEntity);
  }
  // @override
  // void revokeBettingResult(bool? result){
  //   if(result==true){
  //     showToast('成功取消订单');
  //     indentListPagePresenter.getIndentList(widget.index);
  //   }else{
  //     showToast('取消订单失败');
  //   }
  // }

   @override
  void initState() {
    super.initState();
    if(mounted) {
      _onRefresh();
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future<dynamic> _onRefresh() async {
    Future.delayed(Duration(seconds: 1),()=>{indentListPagePresenter.getIndentList(widget.index)});

  }

  Future<dynamic> _loadMore() async {
    indentListPagePresenter.getMoreIndentList(widget.index, provider.currentPage!+1);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<IndentListPageProvider>(
        create: (_) => provider,
        child: Consumer<IndentListPageProvider>(
          builder: (_,provider,child){
            if(provider.stateType==StateType.loading){
              return StateLayout(type: StateType.loading);
            }
            if(provider.list!.length>0) {
              return DeerListView(
                  itemCount: provider.list!.length!,
                  stateType: provider.stateType!,
                  onRefresh: _onRefresh,
                  loadMore: _loadMore,
                  hasMore: provider.hasMore!,
                  itemBuilder: (_, index) {
                    final UserBettingEntity? item = provider.list!.elementAt(
                        index);
                    Text remarkText = Text('');
                    if (widget.index == 1) {
                      remarkText = Text('+' + NumberFormat.decimalPattern('en_us').format(item!.winScore).toString(),
                        style: TextStyle(color: Colors.red),);
                    } else if (widget.index == 2) {
                      remarkText = Text('-' + NumberFormat.decimalPattern('en_us').format(item!.score).toString(),
                        style: TextStyle(color: Colors.green),);
                    }
                    return GestureDetector(
                      onTap: () {
                        NavigatorUtils.push(
                            context, IndexRouter.bettingDetailPage,
                            arguments: item.id.nullSafe);
                      },
                      child: Padding(
                          padding: const EdgeInsets.symmetric(
                              horizontal: 10.0, vertical: 5),
                          child: MyCard(
                              child: Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 5),
                                child: Column(
                                    crossAxisAlignment: CrossAxisAlignment
                                        .start,
                                    children: <Widget>[
                                      SizedBox(height: 5,),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment
                                            .spaceBetween,
                                        children: [
                                          Text('${DeerLocalizations.of(context)!.playWays.nullSafe}:' + (item!.playWay == '1'
                                              ? "${DeerLocalizations.of(context)!.gamePositiveWave.nullSafe}"
                                              : '${DeerLocalizations.of(context)!.gameAntiWave.nullSafe}')),
                                          Text('${DeerLocalizations.of(context)!.orderTime.nullSafe}' +
                                              item!.createOrderTime.nullSafe)
                                        ],
                                      ),
                                      SizedBox(height: 10,),
                                      Row(
                                        children: [
                                          Expanded(child: Row(
                                            children: [
                                              Text('${DeerLocalizations.of(context)!.name.nullSafe}:'),
                                              Container(width: MediaQuery.of(context).size.width/2,child: TextScroll(item!.name.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),),)
                                            ],
                                          )),
                                          Gaps.hGap8,
                                          remarkText
                                        ],
                                      ),
                                      SizedBox(height: 10,),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment
                                            .spaceBetween,
                                        children: [
                                          Visibility(visible: item.bettingType=='1',child: Text('${DeerLocalizations.of(context)!.type.nullSafe}:${DeerLocalizations.of(context)!.score.nullSafe}')),
                                          Visibility(visible: item.bettingType=='2',child: Text('${DeerLocalizations.of(context)!.type.nullSafe}:${DeerLocalizations.of(context)!.winOrLose.nullSafe}')),
                                          Visibility(visible: item.bettingType=='3',child: Text('${DeerLocalizations.of(context)!.type.nullSafe}:${DeerLocalizations.of(context)!.numberOfGoalsScored.nullSafe}')),
                                          Expanded(child: Text('')),
                                          Text('${DeerLocalizations.of(context)!.bettingAmount.nullSafe}:' + NumberFormat.decimalPattern('en_us').format(item!.score).toString())
                                        ],
                                      ),
                                      Visibility(
                                        visible: false,
                                        child: Column(
                                          children: [
                                            SizedBox(height: 5,),
                                            Divider(height: 1.0, indent: 0.0),
                                            SizedBox(height: 5,),
                                            Row(
                                              mainAxisAlignment: MainAxisAlignment
                                                  .spaceBetween,
                                              children: [
                                                Text(''),
                                                EasyButton(
                                                  type: EasyButtonType.outlined,
                                                  idleStateWidget: Text('${DeerLocalizations.of(context)!.cancelOrder.nullSafe}'),
                                                  loadingStateWidget: const CircularProgressIndicator(
                                                    strokeWidth: 3.0,
                                                    valueColor: AlwaysStoppedAnimation<
                                                        Color>(
                                                      Colors.white,
                                                    ),
                                                  ),
                                                  useWidthAnimation: true,
                                                  width: 100,
                                                  height: 30,
                                                  onPressed: () {
                                                    // indentListPagePresenter
                                                    //     .revokeBetting(item.id);
                                                  },
                                                )
                                              ],
                                            )
                                          ],
                                        ),
                                      ),
                                      Visibility(
                                          visible: widget.index == 3,
                                          child: Column(
                                              children: [
                                                SizedBox(height: 5,),
                                                Divider(
                                                    height: 1.0, indent: 0.0),
                                                SizedBox(height: 5,),
                                                Row(
                                                    mainAxisAlignment: MainAxisAlignment
                                                        .spaceBetween,
                                                    children: [
                                                      Text(''),
                                                      Text('撤销时间:' +
                                                          item.updateTime
                                                              .nullSafe)
                                                    ]
                                                )
                                              ]
                                          )
                                      ),
                                      SizedBox(height: 5,),
                                    ]
                                ),
                              )
                          )
                      ),
                    );
                  }
              );
            }else{
              return Container(
                width: MediaQuery.of(context).size.width,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Lottie.asset('assets/lottie/football_game_order.json',width: MediaQuery.of(context).size.width/3,fit: BoxFit.cover),
                      Gaps.vGap16,
                      FadeInText(text: '${DeerLocalizations.of(context)!.noOrderInformation.nullSafe}')
                    ],
                  ),
                ),
              );
            }
          }
        )
    );
  }

  @override
  bool get wantKeepAlive => false;

}